/**
 * 
 */
package com.vision.core.sv.serv;

import java.util.Set;

import net.sf.gilead.core.PersistentBeanManager;
import net.sf.gilead.core.hibernate.HibernateUtil;
import net.sf.gilead.core.serialization.GwtProxySerialization;
import net.sf.gilead.core.store.stateless.StatelessProxyStore;
import net.sf.gilead.gwt.PersistentRemoteService;

import com.vision.core.cm.db.data.Company;
import com.vision.core.cm.db.data.Permission;
import com.vision.core.cm.db.data.User;
import com.vision.core.cm.exception.RpcException;
import com.vision.core.sv.db.CustomSessionFactoryBean;
import com.vision.core.sv.serv.util.ServletUtils;

/**
 * @author Mark
 *
 */
public class BaseRpcServlet extends PersistentRemoteService {

	private static final long serialVersionUID = 7177448229982970076L;
	
	/**
	 * Initializes Gilead, used to allow transfer of Hibernate-bound Data Transfer Objects
	 * from the server to GWT client side.
	 */
	protected void initGilead() {
		/* Start of boilerplate code for Gilead. */
		HibernateUtil gileadHibernateUtil = HibernateUtil.getInstance();
	    gileadHibernateUtil.setSessionFactory( CustomSessionFactoryBean.getFactory() );

	    PersistentBeanManager persistentBeanManager = new PersistentBeanManager();
	    persistentBeanManager.setPersistenceUtil(gileadHibernateUtil);
	    
	    StatelessProxyStore sps = new StatelessProxyStore();
	    sps.setProxySerializer(new GwtProxySerialization());
	    persistentBeanManager.setProxyStore(sps);

	    setBeanManager(persistentBeanManager);
	    /* End of boilerplate code for Gilead. */
	}
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	protected static User getLoggedInUser() {
		return ServletUtils.getLoggedInUser();
	}
	
	protected static void checkHasCompany(Company company) throws RpcException {
		ServletUtils.checkHasCompany(company);
	}
	
	protected static Set<Permission> getLoggedInUsersPermissions() {
		return ServletUtils.getLoggedInUsersPermissions();
	}
	
	protected static void checkPermissions(String ... requiredPermissions) throws RpcException {
		ServletUtils.checkPermissions(false, requiredPermissions);
	}
	
	protected static void checkPermissions(boolean matchAll, String ... requiredPermissions) throws RpcException {
		ServletUtils.checkPermissions(matchAll, requiredPermissions);
	}
	
	protected static void checkPermissions(Permission ... requiredPermissions) throws RpcException {
		ServletUtils.checkPermissions(false, requiredPermissions);
	}
	
	protected static void checkPermissions(boolean matchAll, Permission ... requiredPermissions) throws RpcException {
		ServletUtils.checkPermissions(matchAll, requiredPermissions);
	}
	
}
